<!--
Date: 2025-08-13
Copyright (c) 2025 Industrial Software Feature Database
-->
<template>
  <div class="result-library-op2__main">
    <!-- <div class="left"> -->
      <el-card shadow="never" class="left">
          <el-tree
            ref="materialTree"
            :data="treeData"
            node-key="index"
            highlight-current
            :props="defaultProps"
            :default-expand-all="false"
            :current-node-key="currentChooseId"
            icon-class="el-icon-arrow-right"
            @node-click="treeSelect"
          >
          <template #default="{ data }">
            <span style="display: flex; align-items: center">
              <span class="sub_title"> {{ data.name }} {{ data.versionNumber ? ' / '+data.versionNumber : '' }}</span>
            </span>
          </template>
        </el-tree>
      </el-card>
    <!-- </div> -->
    <div class="right">
      <div class="query">
        <el-form inline ref="queryFormRef" :model="queryParams" label-width="120px" size="small">
          <el-form-item label="上传日期：">
            <el-date-picker
              v-model="dateRangeCreateTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="文件名称：">
            <el-input v-model="queryParams.fileName" clearable />
          </el-form-item>
          <el-form-item class="query_btns">
            <el-button size="small" type="primary" icon="search" @click="doQuery">查询</el-button>
            <el-button size="small" type="default" icon="refresh" @click="doReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="table_div">
        <el-button size="small" type="primary" class="upload_btn" plain icon="Plus" @click="resultStatistics">结果文件分析</el-button>
        <el-table :data="tableData" v-loading="loading" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="left" />
          <el-table-column label="文件名称" align="left" prop="fileName" fixed="left"></el-table-column>
          <el-table-column label="所属轮次" align="left" prop="roundNumber"> </el-table-column>
          <el-table-column label="文件所在位置" align="left" prop="location"> </el-table-column>
          <el-table-column label="状态" width="120" align="left" prop="status"> 
            <template #default="scope">
              <span v-if="scope.row.status == 0">初始化</span>
              <span v-if="scope.row.status == 1">结构化中</span>
              <span v-if="scope.row.status == 2">结构化成功</span>
              <span v-if="scope.row.status == 3">结构化失败</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="left" prop="remarks"> </el-table-column>
          <el-table-column label="上传日期" align="left" width="160" prop="uploadTimeStr"></el-table-column>
          <el-table-column label="操作" align="left" width="100">
            <template #default="scope">
              <el-button size="small" link type="primary" icon="View" @click="downloadOp2File(scope.row)">文件下载</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagenation">
          <!-- <el-pagination
            v-show="total > 0"
            layout="prev, pager, next,jumper"
            v-model:page="queryParams.pageQuery.pageNum"
            v-model:limit="queryParams.pageQuery.pageSize"
            :total="total"
            @current-change="handleCurrentChange"
          /> -->
          <pagination
            v-show="total > 0"
            style="margin-top: 0;"
            v-model:page="queryParams.pageQuery.pageNum"
            v-model:limit="queryParams.pageQuery.pageSize"
            :total="total"
            :background="false"
            layout="total, sizes, prev, pager, next, jumper"
            @pagination="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
import { downloadFile } from '@/utils/index';
import { getOp2Tree, getMeshOp2DataList, downloadOp2ResultFile } from '@/api/dataAcquisition';
// import AllFileView from './AllFileView.vue';
const router = useRouter();
const emits = defineEmits(['handleEvent']);
const props = defineProps(['tableName']);
const tableData = ref([]);
const dialogVisible = ref(false);
const dateRangeCreateTime = ref(['', '']);
const currentChooseId = ref('');
const treeData = ref([]);
const queryParams = ref({
  pageQuery: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 'createTime', 
    isAsc: 'descending'
  },
  fileName: '',
  modelNumber: '',
  batchNumber: '',
  sectionNumber: '',
  qualityCheck: undefined,
  uploadTimeStartTime: '',
  uploadTimeEndTime: ''
});
const total = ref(0);
const loading = ref(false);

const handleCurrentChange = (number) => {
  // queryParams.value.pageQuery.pageNum = number;
  doQuery();
};
const downloadOp2File = (row) => {
  // loading.value = true;
  // downloadOp2ResultFile({_id: row._id}).then(res => {
  //   downloadFile(res, `${row.fileName.split('.')[0]}`, 'op2');
  //   loading.value = false;
  // })
  row.fileUrl && window.open('http://172.16.41.118/file'+row.fileUrl, '_blank')          
}
const resultStatistics = () => {
  router.push({ path: '/dataApplication/op2FileAnalysis' });
}
// 回调函数
const handleEvent = (data) => {
  if (data?.type === 'refresh') {
    doQuery();
    dialogVisible.value = false;
  } else if (data?.type === 'cancel') {
    dialogVisible.value = false;
  } 
};
// tree选择事件
const treeSelect = (data, node) => {
  // console.log(node, data)
  let sectionNumber = '', batchNumber = '', modelNumber = '';
  currentChooseId.value = data.id;
  if (node.level == 1) {
    sectionNumber = '';
    batchNumber = '';
    modelNumber = data.mark;
  } else if (node.level == 2) {
    sectionNumber = '';
    batchNumber = data.mark;
    modelNumber = node.parent.data.mark;
  } else if (node.level == 3) {
    sectionNumber = data.mark;
    batchNumber = node.parent.data.mark;
    modelNumber = node.parent.parent.data.mark;
  }
  doQuery({
    sectionNumber,
    batchNumber,
    modelNumber
  });
};
//查询数据列表
const doQuery = (params = {}) => {
  loading.value = true;
  tableData.value = [];
  queryParams.value.uploadTimeStartTime = dateRangeCreateTime.value[0];
  queryParams.value.uploadTimeEndTime = dateRangeCreateTime.value[1];
  params = {
    ...queryParams.value,
    ...params
  }
  
  getMeshOp2DataList(params)
    .then((res) => {
      // 处理数据
      loading.value = false;
      total.value = res?.total;
      tableData.value = res.rows;
    })
    .catch(() => {
      loading.value = false;
    }).finally(() => {
      loading.value = false;
    });
};
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
};

const doReset = () => {
  queryParams.value = {
    pageQuery: {
      pageNum: 1,
      pageSize: 10,
      orderByColumn: 'createTime',
      isAsc: 'descending'
    },
    modelNumber: '',
    batchNumber: '',
    sectionNumber: '',
    fileName: '',
    qualityCheck: undefined,
    uploadTimeStartTime: '',
    uploadTimeEndTime: ''
  };
  dateRangeCreateTime.value = ['', ''];
  doQuery();
};

//删除
const handleDelete = (row) => {
  const dataIds = row?.id ? [row?.id] : ids.value;
  // deleteBoundaryOptionValidate(dataIds).then(async res => {
  //   let infoMsg = !res.data ? res.msg : (row?.id ? '是否确认删除当前数据项？' : '是否确认删除当前所选全部数据项？')
  //   ElMessageBox.confirm(infoMsg, '提示', {
  //     confirmButtonText: '确定',
  //     cancelButtonText: '取消',
  //     type: 'warning'
  //   }).then(() => {
  //     deleteBoundaryOption(dataIds)
  //     .then((res) => {
  //       if (res) {
  //         ElMessage({ showClose: true, message: '删除成功。', type: 'success' });
  //         doQuery();
  //       }
  //     })
  //     .catch(() => {
  //       ElMessage({ showClose: true, message: '删除失败。', type: 'error' });
  //     });
  //   });
  // })
};
// 装载结果库树数据
const loadTreeData = () => {
  loading.value = true;
  getOp2Tree().then(res => {
    treeData.value = res.data;
    loading.value = false;
  })
};
onMounted(() => {
  loadTreeData();
  doQuery();
});
</script>
<style lang="scss" scoped>
.result-library-op2__main {
  display: flex;
  padding: 20px;
  .left {
    flex: 0 0 300px;
    margin-right: 10px;
    height: calc(100vh - 170px);
    overflow-y: auto;
  }
  .right {
    height: calc(100vh - 170px);
    overflow-y: auto;
    flex: 1;
  }
  .query {
    position: relative;
    border: 1px solid #dddddd7a;
    padding: 20px 10px 0 10px;
    .query_btns {
      padding-left: 100px;
    }
    .return_btn {
      position: absolute;
      top: 30px;
      right: 30px;
      // width: 100%;
      > img {
        height: 45px;
        width: 45px;
        margin-left: 10px;
        cursor: pointer;
      }
    }
  }
  .table_div {
    margin-top: 10px;
    border: 1px solid #dddddd7a;
    padding-bottom: 10px;
    .upload_btn {
      margin: 10px 10px;
    }
    .pagenation {
      display: flex;
      justify-content: end;
      margin-top: 10px;
    }
  }
  :deep(.el-form-item__content) {
    width: 220px;
  }
  :deep(.el-tree-node__label) {
    font-size: 12px;
    color: #606266;
  }
}
</style>
      